<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Announcement;
use app\common\model\Banner;
use app\common\model\Category;
use app\common\model\Institution;
use app\common\model\material\Characters;
use app\common\model\material\Img;
use app\common\model\material\Video;
use app\common\model\shop\Order;
use app\common\model\User;
use app\common\model\user\Channel;
use app\common\model\user\Commission;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * @Author LK
     * @date: 2025-03-10 10:28:07
     * @ApiTitle (测试)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({"code":0,"msg":"返回成功"})
     */
    public function test()
    {
        // $res = Commission::where('type', 'first')->whereTime('createtime', '>', '2025-09-3')->column('order_no');
        $res = [
            // 'NO202509301716047510',
            'NO202509261413195833',
            'NO202509301011332471',
            'NO202509252059021280',
            'NO202510080111043047',
            'NO202510091246176365',
            'NO202509281103517928',
            'NO202509281233334620',
            'NO202509261417416311',
            'NO202510090952188797',
            'NO202509291434364385',
            'NO202509291421057324',
            'NO202510091434190941',
            'NO202510091441560974',
            'NO202510061132416446',
        ];
        Db::startTrans();
        try {
            // $total = collection(Commission::where('type', 'first')->whereTime('createtime', '>', '2025-09-3')->whereIn('order_no', $res)->group('user_id')->field('user_id,sum(money) as total_money')->select())->toArray();
            foreach ($res as $key => $value) {
                $commission = Commission::where('type', 'channel')->where('order_no', $value)->find();
                $order = Order::where('order_no', $value)->find();
                $channeluser = User::where('id', $commission->user_id)->where('is_channel', 1)->find();
                $channel_commission =  Channel::where('id', $channeluser->channel_level)->value('commission_rate');
                if ($channel_commission <= 0) {
                    return;
                }
                $channel = bcdiv($channel_commission, 100, 2);
                if ($channel <= 0) {
                    return;
                }
                $money = bcmul($order->total_fee - $order->only_fx_price, $channel, 2);

                if ($money <= 0) {
                    return;
                }
                $data = [
                    'user_id' => $commission->user_id,
                    'order_no' => $order->order_no,
                    'money' => $money,
                    'msg' => '渠道商佣金',
                    'type' => 'channel',
                    'status' => '1',
                    'source_user_id' => $commission->source_user_id,
                    'source_price' => $commission->source_price,
                    'acct_id' => $commission->acct_id
                ];
                $ress = Commission::create($data);
                if ($ress) {
                    $commission->delete();
                }
            }
            // Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
            return $e->getMessage();
        }
        // $lists = Commission::where('user_id', 954)->where('type', 'first')->whereTime('createtime', '>', '2025-07-10')->column('order_no');
        // // $res = Commission::where('msg', '订单差价')->whereIn('order_no', $order_id)->whereTime('createtime', '>', '1741359299')->find();
        // Db::startTrans();
        // try {
        //     foreach ($lists as $kl => $v) {
        //         $order =  Order::get(['order_no' => $v]);
        //         Commission::addChannelCommission($order);
        //         # code...
        //     }

        //     Db::commit();
        // } catch (\Exception $th) {
        //     Db::rollback();
        //     halt($th);
        //     $this->error($th->getMessage());
        // }
    }
    /**
     * 首页
     *
     */
    public function index()
    {

        $acct_id = $this->auth->acct_id;
        $bannerList = Banner::where('status_switch', '1')->where('acct_id', $acct_id)->order('weigh desc,id desc')->select();
        $announcementList = Announcement::with(['category'])->where('announcement.acct_id', $acct_id)->limit(3)->order('id desc')->select();
        // $about = config('site.about');
        $this->success('请求成功', compact('bannerList', 'announcementList'));
    }
    /**
     * 公告列表
     *
     * @ApiMethod (GET)
     * @param int $category_id  分类id
     */
    public function announcementList()
    {
        $param = request()->param();
        extract($param);
        if (!isset($category_id) || !$category_id) {
            $this->error('参数错误');
        }
        $acct_id = $this->auth->acct_id;
        $data = Announcement::where('category_id', $category_id)->where('acct_id', $acct_id)->order('id desc')->paginate();
        $this->success('', $data);
    }
    /**
     * 公告详情
     *
     * @ApiMethod (GET)
     * @param int $id  公告id
     */
    public function announcementInfo()
    {
        $param = request()->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $data = Announcement::get($id);
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @date: 2024-07-02 
     * @ApiTitle (公告分类)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */

    public function ann_category()
    {
        $acct_id = $this->auth->acct_id;
        $res = Category::where('type', 'ann_category')
            ->where('status', 'normal')
            ->where('acct_id', $acct_id)
            ->order('weigh desc,id desc')
            ->select();
        $this->success('', $res);
    }

    /**
     * @Author LK
     * @date: 2025-02-27 15:54:05
     * @ApiTitle (机构列表)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({"code":0,"msg":"返回成功"})
     */
    public function institution()
    {
        $lists = Institution::order('weigh desc')->select();
        $this->success("请求成功", $lists);
    }
    /**
     * @Author LK
     * @date: 2025-09-15 10:08:40
     * @ApiTitle (素材分类)
     * @ApiMethod (GET)
     * @ApiParams (name="pid", type="integer", required=true, sample="0=一级分类")
     * @ApiReturnParams (name="code", type="string", required=true, description="")
     * @ApiReturn ({"code":1,"msg":"返回成功"})
     */
    public function material_category()
    {
        $params = request()->param();
        extract($params);
        $model = new Category;
        if (isset($pid)) {
            $model->where('pid', $pid);
        }
        $lists = $model->where('type', 'goods')
            ->where('status', 'normal')
            ->order('weigh desc,id desc')
            ->select();
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2025-07-09 10:51:58
     * @ApiTitle (素材库)
     * @ApiMethod (GET)
     * @ApiParams (name="two", type="integer", required=true, sample="", description="二级分类")
     * @ApiParams (name="type", type="integer", required=true, sample="", description="1=图片库,2=视频库,3=文字库")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({"code":1,"msg":"返回成功"})
     */
    public function source_material()
    {
        $params = request()->param();
        extract($params);
        if (empty($two)) {
            $this->error('请选择二级分类');
        }
        if (empty($type)) {
            $this->error('参数错误');
        }
        if ($type == 1) {
            $model = new Img();
        }
        if ($type == 2) {
            $model = new Video();
        }
        if ($type == 3) {
            $model = new Characters();
        }
        $lists = $model->order('id desc')->paginate($limit ?? 10);
        $this->success("请求成功", $lists);
    }
}
